import { RoutersMap } from '@/router/routers';
import { getInfo } from '@/api/passport';

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission(permission, route) {
  if (route.meta && route.meta.permission) {
    let flag = false;
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.includes(permission[i]);
      if (flag) {
        return true;
      }
    }
    return false;
  }
  return true;
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole(roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id);
  }
  return true;
}
//增删改查按钮是否有权限加入到路由meta
function hasRouterBtn(BtnMap, roles) {
  let routers =  BtnMap[0].children
  let permissionList = roles.permission
  routers.forEach(item=>{
    if (item.meta && item.meta.permission) {
      for(let i = 0, len = permissionList.length; i < len; i++){
        if (item.meta.permission.includes(permissionList[i].name)) {
          item.children.forEach(chidItem=>{
            chidItem.meta.permissionBtn = []
            chidItem.meta.permissionBtn = permissionList[i].children.map(names=>{return names.name})
          })
        }
      }
    }
  })
  return BtnMap
}

function filterAsyncRouter(routerMap, roles) {
  const accessedRouters = routerMap.filter((route) => {
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles);
      }
      return true;
    }
    return false;
  });
  
  return accessedRouters;
}
const permission = {
  state: {
    routers: RoutersMap,
    addRouters: [],
    permissionAll: [],
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = routers;
    },
    SET_PermissionAll: (state, permissionData) =>{
      state.permissionAll = permissionData
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const { roles } = data
        
        const accessedRouters = filterAsyncRouter(RoutersMap, roles)
        const accessRouters =  hasRouterBtn(accessedRouters, roles)
        // 隐藏无二级菜单的一级菜单
        // accessedRouters.forEach(item => {
        //   if (item.name == "index") {
        //     if (item.children.length && item.children != undefined ) {
        //       // console.log(item);
        //       item.children.forEach(mapRouter=>{
        //         if (mapRouter.children.length == 0 || mapRouter.children == undefined ) {
        //           mapRouter.hidden = true
        //         }
        //       })
        //     }
        //   }  
        // });
        commit('SET_ROUTERS', accessRouters);
        commit('SET_PermissionAll', roles.permissionList);//所有权限字段
        resolve()
      })
    },
    GetInfo({ commit }) {
      return getInfo()
    },
  },
};

export default permission;
